package team.seekers.catering.user.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import team.seekers.catering.common.utils.CateringUtil;
import team.seekers.catering.user.mapper.UserLogEatMapper;
import team.seekers.catering.user.entity.dao.UserLogEat;
import team.seekers.catering.user.entity.vo.list.UserLogEatListVO;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author hbc
 * @date 2022-3-19
 */
@Service
public class UserLogEatService extends ServiceImpl<UserLogEatMapper, UserLogEat>{

    @Autowired
    private UserService userService;

    public List<UserLogEatListVO> todayLogs(){
        // 获取今日零点到当前时间点
        Calendar calendar = Calendar.getInstance();
        Date now = new Date(calendar.getTimeInMillis());
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Date zero = new Date(calendar.getTimeInMillis());

        QueryWrapper<UserLogEat> wrapper = new QueryWrapper<>();
        wrapper.between("gmt_create",zero,now);
        wrapper.eq("u_id", CateringUtil.getUserId());
        List<UserLogEat> userLogEatList = list(wrapper);

        List<UserLogEatListVO> listVOS = new ArrayList<>();
        for (UserLogEat userLogEat : userLogEatList) {
            UserLogEatListVO listVO = new UserLogEatListVO();
            BeanUtils.copyProperties(userLogEat,listVO);

            SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss");
            listVO.setStartTime(dateFormat.format(userLogEat.getGmtCreate()));
            listVO.setType(userLogEat.getType().getValue());

            listVOS.add(listVO);
        }
        return listVOS;
    }



}
